home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1999 Spring / macformat-077.iso / Shareware Plus / Development / Akua Sweets 131 / Akua Sweets Examples / Internet / IMG W⁄H Reset < prev    next >
Encoding:
Text File  |  1999-03-04  |  7.4 KB  |  315 lines  |  [TEXT/ToyS]

  1. -- Preferences
  2. property kasPrefName : "Width / Height Removal"
  3.  
  4. property kasTypesToDo : {"TEXT"} -- Only run on these types
  5. property kasExtToDo : "" -- Use non-empty match string (e.g. "*.html") to limit to that extension
  6.  
  7. -- Globals
  8. global gasInfoWind -- Info window
  9. global gasInfoPos -- Position of info window
  10. global gasFoldersToDo -- The folders left to process
  11. global gasConverted -- Number gone!
  12. global gasChecked -- Number checked!
  13. global gasImages -- Number images checked!
  14. global gasPrefix -- Adjusted kasPrefix (munge the % parms)
  15. global gasErrorFiles -- List of files with errors
  16. global gasErrorText -- Text list of errors
  17.  
  18.  
  19. on open fsObjs
  20.     -- Load prefs, show window
  21.     pfLoad()
  22.     
  23.     set gasConverted to 0
  24.     set gasChecked to 0
  25.     set gasImages to 0
  26.     set gasErrorFiles to {}
  27.     set gasErrorText to ""
  28.     
  29.     set gasInfoWind to display info titled kasPrefName ¬
  30.         located at gasInfoPos ¬
  31.         message "Scanning…"
  32.     
  33.     -- Do files
  34.     set gasFoldersToDo to {}
  35.     
  36.     repeat with fsObj in fsObjs
  37.         set myInfo to (basic info for fsObj)
  38.         
  39.         if (system type of myInfo is "fold") then
  40.             set gasFoldersToDo to gasFoldersToDo & {fsObj}
  41.         else if (system type of myInfo is in kasTypesToDo) then
  42.             if (kasExtToDo is "") or ((collect lines of (catalog name of myInfo) that match kasExtToDo) is not "") then
  43.                 DoOne(fsObj)
  44.             end if
  45.         end if
  46.     end repeat
  47.     
  48.     -- Do folders
  49.     repeat while gasFoldersToDo is not {}
  50.         -- Pop one off the end
  51.         set n to the number of items of gasFoldersToDo
  52.         set fsObj to item n of gasFoldersToDo
  53.         
  54.         if (n > 1) then
  55.             set gasFoldersToDo to items 1 through (n - 1) of gasFoldersToDo
  56.         else
  57.             set gasFoldersToDo to {}
  58.         end if
  59.         
  60.         ShowToGo(n)
  61.         
  62.         -- Process it
  63.         GoDeep(fsObj)
  64.     end repeat
  65.     
  66.     display info gasInfoWind message "DONE!"
  67.     
  68.     pause for 2 with seconds timing -- Let screen wait...
  69.     
  70.     set gasInfoPos to screen location of ¬
  71.         (display info gasInfoWind with disposal)
  72.     
  73.     pfSave() -- Save window location
  74.     
  75.     if (gasErrorText is not "") then
  76.         clip gasErrorText
  77.         set amenu to (path to apple menu items folder)
  78.         set notepads to the entries in amenu whose creators are in {"npdt"}
  79.         set notePad to item 1 of notepads
  80.         arouse (amenu as string) & notePad
  81.         tell application notePad to activate
  82.         pause for 60
  83.         input state {keys down:"n", command key down:true} -- New note
  84.         input state {keys down:"v", command key down:true} -- Paste errors
  85.         activate
  86.     end if
  87.     
  88.     if (gasErrorFiles is not {}) then
  89.         display dialog "Open files with errors?" default button 2
  90.         tell application "Finder" to open gasErrorFiles
  91.     end if
  92. end open
  93.  
  94.  
  95. on DoOne(fsObj)
  96.     set aFileInfo to (alias info from fsObj)
  97.     ShowChecked(original name of aFileInfo)
  98.     ConvertFile(fsObj)
  99. end DoOne
  100.  
  101.  
  102. on ConvertFile(fsObj)
  103.     try
  104.         set fileData to ¬
  105.             read data from the data fork of fsObj
  106.     on error errStr
  107.         ShowError(fsObj, errStr)
  108.         return
  109.     end try
  110.     
  111.     set fname to catalog name of (basic info for fsObj)
  112.     set html to parse ML fileData
  113.     set imgTags to collect items of html that match "%<IMG*"
  114.     
  115.     if ShowImgCnt(number of items of imgTags) then
  116.         set changed to false
  117.         
  118.         -- Check tags!
  119.         repeat with imgTag in imgTags
  120.             set myParms to item 1 of (parse tag (item imgTag of html))
  121.             set oldW to 0
  122.             set oldH to 0
  123.             set hTags to (collect items of myParms that match "%HEIGHT=*")
  124.             set wTags to (collect items of myParms that match "%WIDTH=*")
  125.             
  126.             -- Got height or widht parms?
  127.             if ShowTagCnt(number of items of hTags, number of items of wTags) then
  128.                 -- Get oldW, oldH!
  129.                 if (hTags is not {}) then set oldH to parse tag val (item (item 1 of hTags) of myParms)
  130.                 if (wTags is not {}) then set oldW to parse tag val (item (item 1 of wTags) of myParms)
  131.                 -- Remove H/W tags from the parsed tag
  132.                 set myParms to (edit list myParms with edits hTags with removal of items)
  133.                 set myParms to (edit list myParms with edits wTags with removal of items)
  134.             end if
  135.             
  136.             -- Find our "SRC=" file…
  137.             set sTag to (collect items of myParms that match "%SRC=*")
  138.             if (sTag is {}) then
  139.                 ShowError(fsObj, "No SRC tag in " & fname)
  140.             else
  141.                 set sTag to item (item 1 of sTag) of myParms
  142.                 
  143.                 try
  144.                     set picFile to (resolve SRC tag sTag relative to file fsObj)
  145.                 on error
  146.                     ShowError(fsObj, "Can't find: " & sTag)
  147.                     set picFile to 0
  148.                 end try
  149.                 
  150.                 if (picFile is not 0) then
  151.                     ShowImageFile(picFile as string)
  152.                     -- Update the HTML
  153.                     set box to picture bounds of (the picture info for (the image from picFile))
  154.                     set w to (item 3 of box) - (item 1 of box)
  155.                     set h to (item 4 of box) - (item 2 of box)
  156.                     if (w is not oldW or h is not oldH) then
  157.                         set changed to true
  158.                         set w to "WIDTH=" & w
  159.                         set h to "HEIGHT=" & h
  160.                         set myParms to myParms & {w, h}
  161.                         set item imgTag of html to (compile tag myParms)
  162.                     end if
  163.                 end if
  164.             end if
  165.         end repeat
  166.         
  167.         -- Rename to lowercase?
  168.         if (changed) then
  169.             -- Write data
  170.             write data to the data fork of fsObj from buffer (compile ML html)
  171.             ShowConverted(fname)
  172.         end if
  173.     end if
  174. end ConvertFile
  175.  
  176.  
  177. on ShowImgCnt(cnt)
  178.     display info gasInfoWind ¬
  179.         message ("Images: " & cnt) ¬
  180.         at line 10
  181.     
  182.     return cnt > 0
  183. end ShowImgCnt
  184.  
  185.  
  186. on ShowTagCnt(cntW, cntH)
  187.     set n to cntW + cntH
  188.     display info gasInfoWind ¬
  189.         message ("Tags: " & n) ¬
  190.         at line 11
  191.     
  192.     return n > 0
  193. end ShowTagCnt
  194.  
  195.  
  196. on ShowError(fsObj, errStr)
  197.     set gasErrorFiles to gasErrorFiles & fsObj
  198.     set gasErrorText to gasErrorText & (fsObj as string) & " • " & errStr & return
  199.     
  200.     display info gasInfoWind ¬
  201.         message ("Error: " & errStr) ¬
  202.         at line 20 ¬
  203.         using color (25 * 1024)
  204. end ShowError
  205.  
  206.  
  207. on ShowConverted(fname)
  208.     set gasConverted to gasConverted + 1
  209.     
  210.     display info gasInfoWind ¬
  211.         message ("Converted: " & gasConverted) ¬
  212.         at line 8 ¬
  213.         using color (15 * 1024)
  214.     
  215.     display info gasInfoWind ¬
  216.         message ("Last: " & fname) ¬
  217.         at line 9 ¬
  218.         using color (16 * 1024)
  219. end ShowConverted
  220.  
  221.  
  222. on ShowChecked(fname)
  223.     set gasChecked to gasChecked + 1
  224.     
  225.     display info gasInfoWind ¬
  226.         message ("File: " & fname) ¬
  227.         at line 6
  228.     
  229.     display info gasInfoWind ¬
  230.         message ("Checked: " & gasChecked) ¬
  231.         at line 7 ¬
  232.         using color 15
  233. end ShowChecked
  234.  
  235.  
  236. on ShowImageFile(fname)
  237.     set gasImages to gasImages + 1
  238.     
  239.     display info gasInfoWind ¬
  240.         message ("Image: " & fname) ¬
  241.         at line 15
  242.     
  243.     display info gasInfoWind ¬
  244.         message ("Images: " & gasImages) ¬
  245.         at line 16
  246. end ShowImageFile
  247.  
  248.  
  249. on ShowAction(msg)
  250.     display info gasInfoWind ¬
  251.         message msg ¬
  252.         at line 2 ¬
  253.         using color (15 * 1024 + 15)
  254. end ShowAction
  255.  
  256.  
  257. on ShowToGo(n)
  258.     display info gasInfoWind ¬
  259.         message ("Folders to go: " & n) ¬
  260.         at line 14 ¬
  261.         using color (15 * 32)
  262. end ShowToGo
  263.  
  264.  
  265. on GoDeep(foldObj)
  266.     display info gasInfoWind ¬
  267.         message "Path: " & (foldObj as string)
  268.     
  269.     set daddy to foldObj as string
  270.     
  271.     -- Do kinds that match
  272.     ShowAction("Scanning files…")
  273.     
  274.     if (kasExtToDo is "") then
  275.         set myItems to the entries in foldObj ¬
  276.             whose types are in kasTypesToDo
  277.     else
  278.         set myItems to the entries in foldObj ¬
  279.             whose types are in kasTypesToDo ¬
  280.             whose names match kasExtToDo
  281.     end if
  282.     
  283.     repeat with myItem in myItems
  284.         DoOne((daddy & myItem) as alias)
  285.     end repeat
  286.     
  287.     -- Do folders
  288.     ShowAction("Scanning subfolders")
  289.     
  290.     set myItems to the entries in foldObj ¬
  291.         whose kinds are a folder
  292.     
  293.     repeat with myItem in myItems
  294.         set gasFoldersToDo to gasFoldersToDo & {(daddy & myItem) as alias}
  295.     end repeat
  296.     
  297.     -- Done
  298.     ShowAction("…")
  299. end GoDeep
  300.  
  301.  
  302. on pfLoad()
  303.     try
  304.         set ourPrefs to (load preference named kasPrefName)
  305.         set gasInfoPos to item 1 of ourPrefs
  306.     on error
  307.         set gasInfoPos to {-1, -1}
  308.     end try
  309. end pfLoad
  310.  
  311.  
  312. on pfSave()
  313.     save preference {gasInfoPos} named kasPrefName
  314. end pfSave
  315.